<?php 
	namespace Admin\Controller;
	use Admin\Model\AuthGroupModel;

	class GnameController extends AdminController{



		/*编辑操作*/
		public function editAc(){
			// var_dump($_POST);
			$editac = M('Gname');
			$editac ->create();

			$id = $_POST['id'];


			// var_dump($_GET);
			$num = $editac ->where(array('id' => $id)) ->save();
			// var_dump($num);

			if ($num > 0 ) {
				$this ->redirect('Gname/show','更改成功');
			}else{
				$this ->error('更新失败');
			}


			// $this ->display('Gname/ht');
		}





		/*显示/不显示*/
		public function dis(){
			$display = M('Gname');
			$id = $_GET['id'];
			$num = $display ->where(array('id'=>$id)) ->save(array('status'=>'0'));
			if($num){
				$this->redirect('Gname/show','上传成功',0);
			}else{
				$this ->error('修改失败');
			}
		}

		public function nodis(){
			$dis = M('Gname');
			$arr= $dis ->where(array('status'=>'1'))->count();

			$id = $_GET['id'];
			if ($arr < 4) {
				$num = $dis ->where(array('id'=>$id)) ->save(array('status'=>'1'));
				if($num){
					$this->redirect('Gname/show','上传成功',0);
				}else{
					$this ->error('修改失败');
				}
			}else{
				$this ->error('可显示已达到上限');
			}
		}


		
		/*广告编辑*/
		public function edit(){
			// var_dump($_GET);
			$edit = M('gname');
			$data = $edit -> where(array('id'=>$_GET['id']))->select();
			$data['0'];

			$this ->assign('arr',$data);

			// var_dump($data);

			$this ->display('Gname/edit');
		}




		/*广告删除操作*/
		public function del(){
			$order= M('gname');
			$id= $_GET['k_id'];
			if($order ->where(array('id'=>$id))->delete()){
				$data = array('status'=>true,'msg' =>'操作成功');

				$this ->ajaxReturn($data,'json');
			}else{
				die($data = array('status'=>true,'msg' =>'操作成功'));
			}
		}







		/*广告展示页面*/
		public function show(){
			// var_dump($_SESSION);
		
			$arr = M('Gname');
			// var_dump($_SESSION);
			// $val= $sh ->select();

			if(!empty($_GET['search'])){
				$map['oname'] = array('like',"%".$_GET['search']."%"); 
	            $map1['oname'] = $_GET['search'];
			}
			
			$count = $arr->where($map)->where($abc)->count();// 查询满足要求的总记录数
			// var_dump($arr);
			$Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
			$Page->setConfig('prev','上一页');
		    $Page->setConfig('next','下一页');
		    $Page->setConfig('last','末页');
		    $Page->setConfig('first','首页');
		    $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show = $Page->show();// 分页显示输出

			foreach($map1 as $key=>$val) {
	        	$Page->parameter[$key] = urlencode($val);
	        }


			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$data = $arr->order('id')->where($map)->where($abc)->limit($Page->firstRow.','.$Page->listRows)->select();

			
			$this->assign('page',$show);// 赋值分页输出
			// var_dump($data);
			// $this -> assign('title','客户信息表');
			$this -> assign('val',$data);
			$this -> display("gname/show");
		}


		// 广告主页/添加页面
		public function index(){
			
			$this ->display('Gname/index');

		}




		
		/*提交广告信息*/
		public function addAction(){
			// var_dump($_POST);
			if(!empty($_SESSION['gid'])){
				$arr = M('gname');
				$num = $arr->where(array('status'=>'1'))->count();
				if ($num < 4) {
					$arr ->create();
					$gid = $_SESSION['gid'];

					$res = $arr ->where(array('id'=>"$gid"))->save();
					// var_dump($res);
					// exit;
					if($res){
						unset($_SESSION['gid']);
						$this->redirect('Gname/index','上传成功');
					}else{
						$this ->error('上传失败');
					}
				}else{
					$_POST['status']='0';
					$arr ->create();
					$gid = $_SESSION['gid'];

					$res = $arr ->where(array('id'=>"$gid"))->save();
					// var_dump($res);
					// exit;
					if($res){
						unset($_SESSION['gid']);
						$this->redirect('Gname/index','上传成功');
					}else{
						$this ->error('上传失败');
					}
				}

				

			}else{
					$this ->error('图片还未添加');
			}

				// $this ->display('Ad/ht');

		}


	/* 文件上传 */
    public function upload(){
		$return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
		/* 调用文件上传组件上传文件 */
		$File = D('File');
		$file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
		$info = $File->upload(
			$_FILES,
			C('DOWNLOAD_UPLOAD'),
			C('DOWNLOAD_UPLOAD_DRIVER'),
			C("UPLOAD_{$file_driver}_CONFIG")
		);

        /* 记录附件信息 */
        if($info){
            $return['data'] = think_encrypt(json_encode($info['download']));
            $return['info'] = $info['download']['name'];
        } else {
            $return['status'] = 0;
            $return['info']   = $File->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /* 下载文件 */
    public function download($id = null){
        if(empty($id) || !is_numeric($id)){
            $this->error('参数错误！');
        }

        $logic = D('Download', 'Logic');
        if(!$logic->download($id)){
            $this->error($logic->getError());
        }

    }


	/**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
	    public function uploadPicture(){
	        //TODO: 用户登录检测

	        /* 返回标准数据 */
	        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

	        /* 调用文件上传组件上传文件 */
	        $Picture = D('Gname');
	        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
	        $info = $Picture->upload(
	            $_FILES,
	            C('PICTURE_UPLOAD'),
	            C('PICTURE_UPLOAD_DRIVER'),
	            C("UPLOAD_{$pic_driver}_CONFIG")
	        ); //TODO:上传到远程服务器

	        /* 记录图片信息 */
	        if($info){
	            $return['status'] = 1;
	            $return = array_merge($info['download'], $return);
	        } else {
	            $return['status'] = 0;
	            $return['info']   = $Picture->getError();
	        }

	        /* 返回JSON数据 */
	        $this->ajaxReturn($return);
	    }

	    /**
     * 修改图片
     * @author huajie <banhuajie@163.com>
     */
	    public function uploadPic(){
	        //TODO: 用户登录检测

	        /* 返回标准数据 */
	        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

	        /* 调用文件上传组件上传文件 */
	        $Picture = D('Gname');
	        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
	        $info = $Picture->upld(
	            $_FILES,
	            C('PICTURE_UPLOAD'),
	            C('PICTURE_UPLOAD_DRIVER'),
	            C("UPLOAD_{$pic_driver}_CONFIG")
	        ); //TODO:上传到远程服务器

	        /* 记录图片信息 */
	        if($info){
	            $return['status'] = 1;
	            $return = array_merge($info['download'], $return);
	        } else {
	            $return['status'] = 0;
	            $return['info']   = $Picture->getError();
	        }

	        /* 返回JSON数据 */
	        $this->ajaxReturn($return);
	    }


	}




 ?>